| Conditions | 19 |
| Paths | > 20000 |
| Total Lines | 159 |
| Code Lines | 95 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like FormValidationAPI.validateJobPoster often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | /* |
||
| 190 | FormValidationAPI.validateJobPoster = function( |
||
| 191 | title_en, title_fr, department_id, branch_en, branch_fr, division_en, division_fr, province_id, city_en, city_fr, open_date_time, |
||
| 192 | close_date_time, start_date, term_qty, remuneration_range_low, remuneration_range_high, classification, clearance_id, language_id) { |
||
| 193 | var valid = true; |
||
| 194 | |||
| 195 | if(!FormValidationAPI.fieldNotEmpty(title_en)){ |
||
| 196 | FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_jobTitle_error", "createJobPoster_jobTitle_error_msg", "Error: No Job Title"); |
||
| 197 | valid = false; |
||
| 198 | } |
||
| 199 | else{ |
||
| 200 | FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_jobTitle_error", "createJobPoster_jobTitle_error_msg", "Error: No Job Title"); |
||
| 201 | } |
||
| 202 | |||
| 203 | if(!FormValidationAPI.fieldNotEmpty(title_fr)){ |
||
| 204 | FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_jobTitle_fr_error", "createJobPoster_jobTitle_fr_error_msg", "Error: No Job Title"); |
||
| 205 | valid = false; |
||
| 206 | } |
||
| 207 | else{ |
||
| 208 | FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_jobTitle_fr_error", "createJobPoster_jobTitle_fr_error_msg", "Error: No Job Title"); |
||
| 209 | } |
||
| 210 | |||
| 211 | if(!FormValidationAPI.fieldNotEmpty(department_id)){ |
||
| 212 | FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_department_error", "createJobPoster_department_error_msg", "Error: No Department Selected"); |
||
| 213 | valid = false; |
||
| 214 | } |
||
| 215 | else{ |
||
| 216 | FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_department_error", "createJobPoster_department_error_msg", "Error: No Department Selected"); |
||
| 217 | } |
||
| 218 | |||
| 219 | if(!FormValidationAPI.fieldNotEmpty(branch_en)){ |
||
| 220 | FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_branch_error", "createJobPoster_branch_error_msg", "Error: No Branch"); |
||
| 221 | valid = false; |
||
| 222 | } |
||
| 223 | else{ |
||
| 224 | FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_branch_error", "createJobPoster_branch_error_msg", "Error: Branch"); |
||
| 225 | } |
||
| 226 | |||
| 227 | if(!FormValidationAPI.fieldNotEmpty(branch_fr)){ |
||
| 228 | FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_branch_fr_error", "createJobPoster_branch_fr_error_msg", "Error: No Branch"); |
||
| 229 | valid = false; |
||
| 230 | } |
||
| 231 | else{ |
||
| 232 | FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_branch_fr_error", "createJobPoster_branch_fr_error_msg", "Error: No Division"); |
||
| 233 | } |
||
| 234 | |||
| 235 | if(!FormValidationAPI.fieldNotEmpty(division_en)){ |
||
| 236 | FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_division_error", "createJobPoster_division_error_msg", "Error: No Division"); |
||
| 237 | valid = false; |
||
| 238 | } |
||
| 239 | else{ |
||
| 240 | FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_division_error", "createJobPoster_division_error_msg", "Error: No Division"); |
||
| 241 | } |
||
| 242 | |||
| 243 | if(!FormValidationAPI.fieldNotEmpty(division_fr)){ |
||
| 244 | FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_division_fr_error", "createJobPoster_division_fr_error_msg", "Error: No Division"); |
||
| 245 | valid = false; |
||
| 246 | } |
||
| 247 | else{ |
||
| 248 | FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_division_fr_error", "createJobPoster_division_fr_error_msg", "Error: No Division"); |
||
| 249 | } |
||
| 250 | |||
| 251 | if(!FormValidationAPI.fieldNotEmpty(province_id)){ |
||
| 252 | FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_province_error", "createJobPoster_province_error_msg", "Error: No Province Selected"); |
||
| 253 | valid = false; |
||
| 254 | } |
||
| 255 | else{ |
||
| 256 | FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_province_error", "createJobPoster_province_error_msg", "Error: No Province Selected"); |
||
| 257 | } |
||
| 258 | |||
| 259 | if(!FormValidationAPI.fieldNotEmpty(city_en)){ |
||
| 260 | FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_city_error", "createJobPoster_city_error_msg", "Error: No City"); |
||
| 261 | valid = false; |
||
| 262 | } |
||
| 263 | else{ |
||
| 264 | FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_city_error", "createJobPoster_city_error_msg", "Error: No City"); |
||
| 265 | } |
||
| 266 | |||
| 267 | /*if(!FormValidationAPI.fieldNotEmpty(city_fr)){ |
||
| 268 | FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_city_fr_error", "createJobPoster_city_fr_error_msg", "Error: No City"); |
||
| 269 | valid = false; |
||
| 270 | } |
||
| 271 | else{ |
||
| 272 | FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_city_fr_error", "createJobPoster_city_fr_error_msg", "Error: No City"); |
||
| 273 | }*/ |
||
| 274 | |||
| 275 | if(!FormValidationAPI.fieldNotEmpty(open_date_time)){ |
||
| 276 | FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_openDate_error", "createJobPoster_openDate_error_msg", "Error: No Open Date/Time"); |
||
| 277 | valid = false; |
||
| 278 | } |
||
| 279 | else{ |
||
| 280 | FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_openDate_error", "createJobPoster_openDate_error_msg", "Error: No Open Date/Time"); |
||
| 281 | } |
||
| 282 | |||
| 283 | if(!FormValidationAPI.fieldNotEmpty(close_date_time)){ |
||
| 284 | FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_closeDate_error", "createJobPoster_closeDate_error_msg", "Error: No Close Date/Time"); |
||
| 285 | valid = false; |
||
| 286 | } |
||
| 287 | else{ |
||
| 288 | FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_closeDate_error", "createJobPoster_closeDate_error_msg", "Error: No Close Date/Time"); |
||
| 289 | } |
||
| 290 | |||
| 291 | if(!FormValidationAPI.fieldNotEmpty(start_date)){ |
||
| 292 | FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_startDate_error", "createJobPoster_startDate_error_msg", "Error: No Start Date"); |
||
| 293 | valid = false; |
||
| 294 | } |
||
| 295 | else{ |
||
| 296 | FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_startDate_error", "createJobPoster_startDate_error_msg", "Error: No Start Date"); |
||
| 297 | } |
||
| 298 | |||
| 299 | if(!FormValidationAPI.fieldNotEmpty(term_qty)){ |
||
| 300 | FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_termQuantity_error", "createJobPoster_termQuantity_error_msg", "Error: No Term Duration"); |
||
| 301 | valid = false; |
||
| 302 | } |
||
| 303 | else{ |
||
| 304 | FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_termQuantity_error", "createJobPoster_termQuantity_error_msg", "Error: No Term Duration"); |
||
| 305 | } |
||
| 306 | |||
| 307 | if(!FormValidationAPI.fieldNotEmpty(remuneration_range_low)){ |
||
| 308 | FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_remunerationLowRange_error", "createJobPoster_remunerationLowRange_error_msg", "Error: No minimum salary"); |
||
| 309 | valid = false; |
||
| 310 | } |
||
| 311 | else{ |
||
| 312 | FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_remunerationLowRange_error", "createJobPoster_remunerationLowRange_error_msg", "Error: No minimum salary"); |
||
| 313 | } |
||
| 314 | |||
| 315 | if(!FormValidationAPI.fieldNotEmpty(remuneration_range_high)){ |
||
| 316 | FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_remunerationHighRange_error", "createJobPoster_remunerationHighRange_error_msg", "Error: No maximum salary"); |
||
| 317 | valid = false; |
||
| 318 | } |
||
| 319 | else{ |
||
| 320 | FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_remunerationHighRange_error", "createJobPoster_remunerationHighRange_error_msg", "Error: No maximum salary"); |
||
| 321 | } |
||
| 322 | |||
| 323 | if(!FormValidationAPI.fieldNotEmpty(classification)){ |
||
| 324 | FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_classification_error", "createJobPoster_classification_error_msg", "Error: No Classification"); |
||
| 325 | valid = false; |
||
| 326 | } |
||
| 327 | else{ |
||
| 328 | FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_classification_error", "createJobPoster_classification_error_msg", "Error: No Classification"); |
||
| 329 | } |
||
| 330 | |||
| 331 | if(!FormValidationAPI.fieldNotEmpty(clearance_id)){ |
||
| 332 | FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_clearance_error", "createJobPoster_clearance_error_msg", "Error: No Security Clearance Selected"); |
||
| 333 | valid = false; |
||
| 334 | } |
||
| 335 | else{ |
||
| 336 | FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_clearance_error", "createJobPoster_clearance_error_msg", "Error: No Security Clearance Selected"); |
||
| 337 | } |
||
| 338 | |||
| 339 | if(!FormValidationAPI.fieldNotEmpty(language_id)){ |
||
| 340 | FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_language_error", "createJobPoster_language_error_msg", "Error: No Language Selected"); |
||
| 341 | valid = false; |
||
| 342 | } |
||
| 343 | else{ |
||
| 344 | FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_language_error", "createJobPoster_language_error_msg", "Error: No Language Selected"); |
||
| 345 | } |
||
| 346 | |||
| 347 | return valid; |
||
| 348 | } |
||
| 349 | |||
| 364 |
This check looks for functions where a
returnstatement is found in some execution paths, but not in all.Consider this little piece of code
The function
isBigwill only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly returnundefined.This behaviour may not be what you had intended. In any case, you can add a
return undefinedto the other execution path to make the return value explicit.